<?php

include_once 'HelperDAO.class.php';

class UserDAO {

    /**
     * @var $helperDAO HelperDAO
     */
    private $helperDAO;

    /**
     *
     */
    function __construct($config=false) {
        $this->helperDAO = new HelperDAO($config);
    }

    /**
     *
     * @param String $email
     * @param String $pass
     */
    function checkUser($email, $pass) {
        $dataSet = $this->helperDAO->select("SELECT * from user where email='%s' and password='%s'", array($email, $pass), true);
        $dataSet->role = $this->helperDAO->select("SELECT * from user_role where id = '%s'", array($dataSet->roleId), true);
        return $dataSet;
    }

    /**
     *
     * @param String $email
     */
    function forgetPassword($email) {
        $dataSet = $this->helperDAO->select("SELECT * from user where email='%s'", array($email),true);
        $dataSet->rol = $this->helperDAO->select("SELECT * from user_roles where id = '%s'", array($dataSet->role_id),true);
        return $dataSet;
    }

    /**
     *
     * @param String $email
     * @param String $password
     * @param String $firstName
     * @param String $lastName
     * @param String $gender
     * @param String $birthday
     * @param String $roleId
     */
    function addUser($email, $password, $firstName, $lastName, $mobile, $roleId) {
        return $this->helperDAO->insert(
                "INSERT INTO
			 user(email,password,firstName,lastName,mobile,roleId) 
			 VALUES ('%s','%s','%s','%s','%s','%s')",
                array($email, $password, $firstName, $lastName, $mobile, $roleId));
    }

    /**
     *
     * @param Integer $id
     * @param String $firstName
     * @param String $lastName
     * @param String $gender
     * @param String $birthday
     * @param String $phone
     * @param String $mobile_1
     * @param String $mobile_2
     * @param String $mobile_3
     * @param String $bio
     * @param String $FBLink
     * @param String $address
     */
    function editUserDetails($email, $password, $firstName, $lastName, $mobile,$id,$userRole) {
        return $this->helperDAO->update(
                "UPDATE user
			SET 
				email           = '%s',
				password 	= '%s',
				firstName 	= '%s',
				lastName 	= '%s',
				mobile 		= '%s',
                                roleId          = '%s'
			WHERE
				id		='%s' ",
                array($email, $password, $firstName, $lastName, $mobile,$userRole,$id));
    }

    /**
     *
     * @param Integer $id
     */
    public function get($id) {
        return $this->helperDAO->select("SELECT * FROM user where id = '%s'", array($id), true);
    }

    public function listAll() {
        return $this->helperDAO->select("SELECT * FROM user where roleId = '2'",array());
    }

    public function getMails($userIds) {
        $sqlQuery = "SELECT email FROM user WHERE ";
        $i = 1;
        foreach ($userIds as $userId) {
            $sqlQuery.= "id = '%s'";
            if ($i < count($userIds)) {
                $sqlQuery.= " or ";
            }
            $i++;
        }
        return $this->helperDAO->select($sqlQuery, $userIds);
    }

    public function listUserRoles(){
        return $this->helperDAO->select("SELECT * FROM user_role where  id != 1");
    }

}